<template>
  <div>
    <el-dialog
      :model-value="visible"
      title="商品详情"
      width="65%"
      style="border-radius: 10px"
      @close="cls"
    >
      <div v-if="data" class="product-detail">
        <!-- 基本信息区域 -->
        <div class="basic-info">
          <div class="image-section">
            <!-- 主图 -->
            <el-image
              :src="data.picture"
              :preview-src-list="[data.picture]"
              class="product-image"
              fit="cover"
            >
              <template #error>
                <div class="image-slot">
                  <el-icon><picture-filled /></el-icon>
                </div>
              </template>
            </el-image>
          </div>

          <div class="info-section">
            <el-descriptions :column="1" border>
              <el-descriptions-item label="商品名称">
                {{ data.name }}
              </el-descriptions-item>
              <el-descriptions-item label="商品价格">
                <span class="price">¥{{ data.price }}</span>
              </el-descriptions-item>
              <el-descriptions-item label="商品类别">
                {{ data.className }}
              </el-descriptions-item>
              <el-descriptions-item label="库存数量">
                {{ data.num }}
              </el-descriptions-item>
              <el-descriptions-item label="当前持有人">
                {{ data.buy_name }}
              </el-descriptions-item>
              <el-descriptions-item label="上架状态">
                <el-tag :type="data.status === 1 ? 'success' : 'info'">
                  {{ data.status === 1 ? "已上架" : "已下架" }}
                </el-tag>
              </el-descriptions-item>
            </el-descriptions>
          </div>
        </div>

        <!-- 商品描述区域 -->
        <div class="description-section">
          <h3>商品描述</h3>
          <div class="description-content">
            {{ data.blemish || "暂无描述" }}
          </div>
        </div>

        <!-- 轮播图展示 -->
        <div v-if="data.carouselList?.length > 0" class="image-section">
          <h3>轮播图展示</h3>
          <div class="image-grid">
            <el-image
              v-for="(item, index) in data.carouselList"
              :key="index"
              :src="item.url"
              :preview-src-list="getPreviewList(data.carouselList)"
              fit="cover"
              class="grid-image"
            />
          </div>
        </div>

        <!-- 详情图展示 -->
        <div v-if="data.detailImageList?.length > 0" class="image-section">
          <h3>商品详情图</h3>
          <div class="image-grid">
            <el-image
              v-for="(item, index) in data.detailImageList"
              :key="index"
              :src="item.url"
              :preview-src-list="getPreviewList(data.detailImageList)"
              fit="cover"
              class="grid-image"
            />
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, defineProps, defineEmits } from "vue";
import { PictureFilled } from "@element-plus/icons-vue";

const data = ref({});

// 定义属性
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
});

// 声明事件
const emits = defineEmits(["close", "edit"]);

// 关闭方法
function cls() {
  emits("close");
}

// 编辑方法
function handleEdit() {
  emits("edit", data.value);
}

// 获取预览图片列表
function getPreviewList(imageList) {
  return imageList?.map(item => item.url) || [];
}

// 初始化方法
function init(productData) {
  data.value = productData;
}

// 暴露初始化方法
defineExpose({ init });

// 轮播图高度
const carouselHeight = "300px";
</script>

<style scoped>
.product-detail {
  padding: 10px;
}

.basic-info {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
}

.image-section {
  margin-bottom: 20px;
}

.image-section:last-child {
  margin-bottom: 0;
}

.product-image {
  width: 300px;
  height: 300px;
  border-radius: 8px;
  border: 1px solid #ebeef5;
}

.info-section {
  flex: 1;
}

.price {
  color: #f56c6c;
  font-size: 20px;
  font-weight: bold;
}

h3 {
  margin-bottom: 12px;
  padding-left: 10px;
  border-left: 4px solid #409eff;
  font-size: 16px;
}

.description-content {
  padding: 12px;
  background-color: #f8f9fa;
  border-radius: 4px;
  min-height: 60px;
  line-height: 1.6;
  margin-bottom: 20px;
}

.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px;
}

.grid-image {
  width: 100%;
  height: 100px;
  border-radius: 4px;
  border: 1px solid #ebeef5;
  cursor: pointer;
}

.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 30px;
}

:deep(.el-descriptions__label) {
  width: 100px;
  justify-content: flex-end;
}

.dialog-footer {
  text-align: right;
  padding-top: 10px;
}
</style>
